package one.bugu.android.demon.ui.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.widget.RelativeLayout;

import com.juefeng.android.framework.update.AppVersionUpdateService;
import com.juefeng.android.framework.view.annotation.LKContentView;
import com.juefeng.android.framework.view.annotation.LKInjectView;

import one.bugu.android.demon.BuildConfig;
import one.bugu.android.demon.R;
import one.bugu.android.demon.bean.CheckUpdateBean;
import one.bugu.android.demon.common.MyBaseActivity;
import one.bugu.android.demon.constant.HttpConstant;
import one.bugu.android.demon.util.PreferencesUtil;
import one.bugu.android.demon.util.StatusBarUtils;
import one.bugu.android.demon.util.VersionUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 启动页面
 */
@LKContentView(R.layout.activity_welocome)
public class WelcomeActivity extends MyBaseActivity {

    @LKInjectView(R.id.rl_main_layout)
    private RelativeLayout mMainLayout;

    private final long SPLASH_LENGTH = 100;
    Handler handler = new Handler();

    @Override
    protected void initView() {
        super.initView();
        StatusBarUtils.getInstance().setStatusBar(this, null);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //检测版本
        String verName = VersionUtils.getVerName(WelcomeActivity.this);
        if (verName == null) {
            verName = "";
        }
        Map<String, String> map = new HashMap<>();
        map.put("appId", HttpConstant.APPID);
        map.put("verCode", verName.replace(".", ""));
        map.put("channel", BuildConfig.FLAVOR);
        AppVersionUpdateService appVersionUpdateService = new AppVersionUpdateService(WelcomeActivity.this,
                HttpConstant.CHECK_DEAL, map, CheckUpdateBean.class, new AppVersionUpdateService.OnUpdateListener() {
            @Override
            public void success(Object o) {
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri content_url = Uri.parse(((CheckUpdateBean) o).getUrl());
                intent.setData(content_url);
                startActivity(intent);
            }

            @Override
            public void nothing() {
                startMain();
            }
        });
        appVersionUpdateService.update();
    }


    @Override
    protected void initData() {
        super.initData();

    }


    private void startMain() {
        //判断是否登录状态
        boolean isFirst = PreferencesUtil.getInstance().getBoolean(WelcomeActivity.this, "isFirst");

        //登录状态 跳转预警主界面
        if (isFirst) {
            handler.postDelayed(new Runnable() {  //使用handler的postDelayed实现延时跳转
                public void run() {
                    Intent intent = new Intent(WelcomeActivity.this, WarnMainActivity.class);
                    startActivity(intent);
                    finish();
                }
            }, SPLASH_LENGTH);//3秒后跳转至预警主界面WarnMainActivity
            //finish();
        } else {
            //没登录状态 跳转登录界面
            handler.postDelayed(new Runnable() {  //使用handler的postDelayed实现延时跳转

                public void run() {
                    Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }
            }, SPLASH_LENGTH);//3秒后跳转至登录界面MainActivity
        }
    }
}
